package sockethomework;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @description: TODO
 * @author yumo
 * @version: 1.0
 * @date 2025/3/18 18:50
 */
public class Server {
    private ServerSocket serverSocket;

    public Server() {

        try {
            System.out.println("正在启动服务端...");
            //创建ServerSocket时需要在指定端口，端口不能与其他程序一致，否则会抛出异常java.net.BindException:address already in use
            serverSocket = new ServerSocket(8081);
            System.out.println("启动完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        try {
            while (true) {
                System.out.println("等待客户连接");
                Socket socket = serverSocket.accept();
                System.out.println("客户连接成功");
                ClientHandler handler = new ClientHandler(socket);
                Thread t = new Thread(handler);
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }

    private class ClientHandler implements Runnable {
        private Socket socket;

        public ClientHandler(Socket socket) {
            this.socket = socket;
        }

        public void run() {
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);
                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println("客户端说：" + message);

                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
